// pages/error/error.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    errorList:[],
    loadingText: "加载中...",
    page: 0,
    page_count: "",
    showPage: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },
  getErrorList(){
    let obj = {
      page: this.data.page,
      size: 8
    }
    app.api.getErrorList(obj, app.globalData.userId)
    .then(res => {
      wx.hideLoading()
      let errorLists = res.content;
      console.log(errorLists)
      let errorList = []
      errorLists.forEach(item => {
        if (item.question.answer != item.answer) {
          errorList.push(item)
        }
      })
      errorList.forEach(item => {
        item.title = JSON.parse(item.question.body).title
        item.videoSrc = JSON.parse(item.question.body).videoSrc || ""
        item.imgSrc = JSON.parse(item.question.body).imgSrc || ""
        item.audioSrc = JSON.parse(item.question.body).audioSrc || ""
        item.trueAnswer = this.formatVal(item.question.answer)
        item.errorAnswer = this.formatVal(item.answer)
        item.type = item.question.type
        item.explanation = item.question.explanation
        let trueAnswer = item.question.answer
        let options = item.question.options
        let arr = []
        // if (item.type == 1){
        options.forEach((item,index) => {
          arr.push({
            title: this.formatOpt(index + 1) + item,
            checked: trueAnswer.indexOf(String(index)) != -1,
            // error: trueAnswer.indexOf(String(index)) = -1,
          })
        })
        item.options = arr          
        // }

      })
      this.setData({
        showLoading: false,
        showPage: true,
        errorList: this.data.errorList.concat(errorList),
        page_count: res.totalPages
      })
    })
  },
  formatVal(val) {
    return val ? val.replace(/0|1|2|3|4|5|6|false|true/g, function (val) {
      switch (val) {
        case "0":
          return "A";
          break;
        case "1":
          return "B";
          break;
        case "2":
          return "C";
          break;
        case "3":
          return "D";
          break;
        case "4":
          return "E";
          break;
        case "5":
          return "F";
          break;
        case "6":
          return "G";
          break;
        case 7:
          return "H "
          break;
        case 8:
          return "I "
          break;
        case 9:
          return "J "
          break;
        case 10:
          return "K "
          break;
        case 11:
          return "L "
          break;
        case 12:
          return "M "
          break;
        case 13:
          return "N "
          break;
        case 14:
          return "O "
          break;
        case "false":
          return "错误";
          break;
        case "true":
          return "正确";
          break;
      }
    }): ''
  },
  formatOpt(index) {
    switch (index) {
      case 1:
        return "A "
        break;
      case 2:
        return "B "
        break;
      case 3:
        return "C "
        break;
      case 4:
        return "D "
        break;
      case 5:
        return "E "
        break;
      case 6:
        return "F "
        break;
      case 7:
        return "G "
        break;
      case 7:
        return "G "
        break;
      case 8:
        return "H "
        break;
      case 9:
        return "I "
        break;
      case 10:
        return "J "
        break;
      case 11:
        return "K "
        break;
      case 12:
        return "L "
        break;
      case 13:
        return "M "
        break;
      case 14:
        return "N "
        break;
      case 15:
        return "O "
        break;
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.showLoading({ mask: true })
    this.setData({
      page: 0,
      errorList: [],
      showPage: false
    })
    this.getErrorList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    // console.log(this.data.page, this.data.page_count)
    if (this.data.page+1 == this.data.page_count) {
      console.log(1)
      this.setData({
        showLoading: true,
        loadingText: "已经到底了！"
      })
    } else {
      // setTimeout(function () {
      this.setData({
        page: this.data.page + 1,
        showLoading: true,
        loadingText: "加载中..."
      })
      this.getErrorList()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})